﻿#region Copyright and Licensing Notice
// Copyright (C) 2011 Steven Bosnick
// 
// This program is open source software; you can redistribute it 
// and/or modifiy it under the terms of the Eclipse Public License,
// version 1.0.
// 
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A 
// PARTICULAR PURPOSE. See the Eclipse Public License for more
// details.
// 
// You should have received a copy of the Eclipse Public License 
// along with this program; if not, you can find a copy at
// "http://www.opensource.org/licenses/eclipse-1.0.php".
#endregion
using System;
using BitwiseOre.SharedMemory.Utils;
using Moq;
using NUnit.Framework;

namespace BitwiseOre.Tests.SharedMemory.Utils
{
    [TestFixture]
    public sealed class DisposerFixture : AssertionHelper
    {
        [Test(Description = "Checks that disposing of the disposer delegates the call to the inner IDisposable")]
        public void Dispose_DelegatesToInnerDisposable()
        {
            var disposable = new Mock<IDisposable>();
            var disposer = new Disposer<object>(disposable.Object);
            disposer.Dispose();

            disposable.Verify(d => d.Dispose(), "Dispose() was not called on the inner disposable.");
        }

        [Test(Description = "Ensures that calling ThrowIfDisposed() before disposal does nothing.")]
        public void ThrowIfDisposed_BeforeDispose()
        {
            var disposer = new Disposer<object>(new Mock<IDisposable>().Object);

            // The correct operation of the test is for this to do nothing, 
            // espcially, not throw an exception.
            disposer.ThrowIfDisposed();
        }

        [Test(Description = "Ensures that calling ThrowIfDisposed() after disposal throws an exception.")]
        public void ThrowIfDisposed_AfterDispose()
        {
            var disposer = new Disposer<TestObject>(new Mock<IDisposable>().Object);
            disposer.Dispose();

            Expect(
                () => disposer.ThrowIfDisposed(),
                Throws.InstanceOf<ObjectDisposedException>()
                    .With.Property("ObjectName").EqualTo("TestObject"));
        }

        private class TestObject {}
    }
}